package com.eddie.community.util;

import com.alibaba.fastjson.JSONObject;
import com.eddie.community.aspect.ServiceLogAspect;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * ClassName: CommunityUtil
 * Package: com.eddie.community.util
 * Description:工具类
 * Date: 2021/1/12 0012 22:43
 * Author: eddie_zhan@yeah.net
 */
public class CommunityUtil {
    //生成随机字符串不包含"-"
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    //MD5加密
    public static String md5(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

    /*不同情况的方法*/
    public static String getJSONString(int code, String msg, Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("msg", msg);
        if (map != null) {
            for (String key : map.keySet()) {
                json.put(key, map.get(key));
            }
        }
        return json.toJSONString();
    }

    public static String getJSONString(int code, String msg) {
        return getJSONString(code, msg, null);
    }

    public static String getJSONString(int code) {
        return getJSONString(code, null, null);
    }

    private static final Logger logger = LoggerFactory.getLogger(CommunityUtil.class);

    /**
     * Description: vo冒泡排序,根据排序方式
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @params [voList]
     * @date 2021/3/3 0003 16:59
     * @Author Eddie.Zhan
     */
    public static List<Map<String, Object>> reverseOrder(List<Map<String, Object>> voList) {
        for (int i = 0; i < voList.size() - 1; i++) {
            for (int j = 1; j < voList.size() - i; j++) {
                //取出相邻两个评论的点赞数 转换为int
                Integer var1 = Integer.parseInt(String.valueOf(voList.get(j - 1).get("likeCount")));
                Integer var2 = Integer.parseInt(String.valueOf(voList.get(j).get("likeCount")));

                //取出相邻两个评论的发布时间
                String var3 = String.valueOf(voList.get(j-1).get("createTime"));
                String var4 = String.valueOf(voList.get(j).get("createTime"));

                // 比较两个评论点赞数的大小
                if (var1.compareTo(var2) < 0) {
                    Map<String, Object> temp = voList.get(j - 1);
                    voList.set((j - 1), voList.get(j));
                    voList.set(j, temp);
                }else {
                    if (var3.compareTo(var4)>0){
                        Map<String, Object> temp = voList.get(j - 1);
                        voList.set((j - 1), voList.get(j));
                        voList.set(j, temp);
                    }
                }
            }
        }
        return voList;
    }
}
